<template>
  <el-dialog
    :title="title"
    :visible.sync="innerVisible"
    width="600px"
    @close="handleCancel"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      label-width="180px"
    >
      <el-form-item label="所属园区" prop="parkId">
        <el-select
          placeholder="所属园区"
          v-model="form.parkId"
          clearable
          :disabled="isDetail"
        >
          <el-option
            v-for="item in parkOptions"
            :key="item.pkid"
            :label="item.name"
            :value="item.pkid"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="楼宇" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入"
          :disabled="isDetail"
        />
      </el-form-item>

      <el-form-item label="建筑面积(平方米)">
        <el-input
          v-model="form.buildArea"
          type="number"
          placeholder="请输入"
          :disabled="isDetail"
        />
      </el-form-item>

      <el-form-item label="总可租面积(平方米)">
        <el-input
          v-model="form.rentArea"
          type="number"
          placeholder="请输入"
          :disabled="isDetail"
        />
      </el-form-item>

      <el-form-item label="资产类型" prop="remark">
        <el-select
          placeholder="资产类型"
          v-model="form.remark"
          clearable
          :disabled="isDetail"
        >
          <el-option
            v-for="item in assetsOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="楼层说明">
        <el-input
          v-model="form.explain"
          placeholder="请输入"
          :disabled="isDetail"
          type="textarea"
        />
      </el-form-item>

      <!-- <el-form-item label="入驻企业数">
        <el-input
          v-model="form.enterprise"
          placeholder="请输入"
          :disabled="isDetail"
        />
      </el-form-item> -->

      <el-form-item label="责任单位" prop="unit">
        <!-- <el-select
          placeholder="责任单位"
          v-model="form.unit"
          clearable
          :disabled="isDetail"
        >
          <el-option
            v-for="item in responseOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select> -->
        <el-cascader
          v-model="form.unit"
          :options="responseOptions"
          :disabled="isDetail"
          :props="{
            checkStrictly: true,
            emitPath: false,
            label: 'deptName',
            value: 'deptName',
            children: 'childTree'
          }"
          clearable
          placeholder="请选择"
        />
      </el-form-item>

      <el-form-item label="是否参与考核">
        <el-radio-group
          v-model="form.checkFlag"
          :disabled="isDetail"
        >
          <el-radio label="是"></el-radio>
          <el-radio label="否"></el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <div
      slot="footer"
      class="dialog-footer"
      v-if="!isDetail"
    >
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import {
  parkOptions,
  assetsOptions
  // responseOptions
} from '@/utils/project/data'
import { queryParkInfo } from '@/api/dataManagement'
import { getDeptTree } from '@/api/dept'
import { RESPONSE_DEPT_NAME } from '@/utils/project/data'
import { findPathByValue } from '@/utils/tree'

export default {
  name: 'ProgressDialog',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      default: '填写信息'
    },
    isDetail: {
      type: Boolean
    },
    currentData: {
      type: Object
    }
  },
  data() {
    return {
      form: {
        parkId: '',
        pkid: '',
        name: '',
        remark: '商业载体',
        unit: '',
        checkFlag: '是',
        type: '商业载体'
      },
      rules: {
        parkId: [
          {
            required: true,
            message: '请选择园区',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入楼宇',
            trigger: 'blur'
          }
        ],
        remark: [
          {
            required: true,
            message: '请选择资产类型',
            trigger: 'blur'
          }
        ],
        unit: [
          {
            required: true,
            message: '请选择责任单位',
            trigger: 'blur'
          }
        ]
      },
      innerVisible: false,
      yearList: ['2025', '2024'],
      parkOptions: [],
      responseOptions: []
    }
  },
  computed: {
    // parkOptions() {
    //   return parkOptions
    // },
    assetsOptions() {
      return assetsOptions
    }
    // responseOptions() {
    //   return responseOptions
    // }
  },
  async mounted() {
    this.getList()
    await this.getTreeList()
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
    innerVisible(val) {
      this.$emit('update:visible', val)
    },
    currentData(val) {
      this.form = val
      // if (val.unit) {
      //   const unit = findPathByValue(this.responseOptions, val.unit)
      //   unit.length? this.form.unit = unit : this.form.unit = []
      // } else {
      //   this.form.unit = []
      // }
      // this.form.unit = val.unit
      if (!val.pkid) {
        this.form.type = '商业载体'
      }
    }
  },
  methods: {
    async getTreeList() {
      const res = await getDeptTree()
      if (res.success) {
        const target = res.data[0].childTree.find(
          (i) => i.deptName === RESPONSE_DEPT_NAME
        )

        if (target) {
          this.responseOptions = this.removeEmptyChildTree([
            target
          ])[0].childTree
        } else {
          this.responseOptions = []
        }
      }
    },
    removeEmptyChildTree(nodes) {
      return nodes
        .map((node) => {
          if (Array.isArray(node.childTree)) {
            node.childTree = this.removeEmptyChildTree(
              node.childTree
            )
            if (node.childTree.length === 0) {
              delete node.childTree
            }
          }
          return node
        })
        .filter((node) => {
          return (
            !node.childTree ||
            (Array.isArray(node.childTree) &&
              node.childTree.length > 0)
          )
        })
    },
    getList() {
      const params = {
        pageIndex: 1,
        pageSize: 999
      }
      queryParkInfo(params).then((res) => {
        if (res.success) {
          this.parkOptions = res.data.list
        }
      })
    },
    handleCancel() {
      this.$emit('update:visible', false) // 父组件控制 visible
    },
    handleConfirm() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // if (this.form.unit) {
          //   this.form.unit = this.form.unit.join(',')
          // } else {
          //   this.form.unit = ''
          // }
          this.$emit('confirm', { ...this.form })
          this.handleCancel()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/css/dialog.scss';
::v-deep .el-input__inner {
  width: 320px !important;
}
::v-deep .el-textarea__inner {
  width: 320px !important;
}
</style>
